/**
 * Created by Administrator on 14-8-21.
 */
var debitCardArray = new Array();
var creditCardArray = new Array();
var changExCardFlag = false;
$(function(){
    $.ajaxSetup({cache:false});

   //$("#bankCardNo").keyup(processValueChange);
    //将keyup改为 文本改变即触发事件。 update  by dongdh  20160325
    $(document).on('input propertychange', '#bankCardNo', processValueChange);
    
    expressSelInit();

    $(':button').removeAttr("disabled");
});

//卡号文本框输入 值改变事件函数
function processValueChange() {

    $("#notSupportTip").hide();
    $("#freezeTip").hide();

    var num = /^\d*$/;  //全数字
    var str = this.value;
    var laststr = str.charAt(str.length - 1);
    if (!num.exec(laststr)) {
        this.value=this.value.substr(0,str.length - 1);
        return false;
    }
    this.value =this.value.replace(/\s/g,'').replace(/(\d{4})(?=\d)/g,"$1 ");
    avalPayCheckInit();
    var bankCardNo = this.value.replace(/\s+/g, "");
    if($.trim(bankCardNo).length >=10){
        checkCardNo(bankCardNo);
        changExCardFlag = true;
    }else{
        $("#payBankTr").hide();
        $("#exPayChannel").val("");
        $("#exPayBankCode").val("");
    }

}



function expressSelInit(){
    if($("#exCardId_1").length > 0){
        var epc = $("#exPayChannel1").val();
        var epb = $("#exPayBankCode1").val();
        var bcn = $("#bankCardNo1").val();
    /*    if($("#firstExpressView").length >0 && $("#firstExpressView").css("display") != "none" ){
            //更换虚拟帐号数据初始化问题
            bcn = "";
        }*/
        reUseBindCard();
        exBankSel("1",epc,epb,bcn);
    }
}

function checkSms(formId){
    var formData = $("#"+formId+"").getFormData();
    //处理隐藏字段
    //处理星号的字段 exholder  和    idcard
      var exHolder = $.trim($('#exHolder').attr('initData'));
      if (exHolder != '') {
      	formData.exHolder = exHolder;
      }
      
      var exIdCardNo = $.trim($('#exIdCardNo').attr('initData'));
      if (exIdCardNo != '') {
      	formData.exIdCardNo = exIdCardNo;
      }
    var res = false;
    $("#exSmsCodeTip").hide();

    bodyRSA();
    var result;
    if($('#exCvn2').length && $('#exCvn2').length>0){
        result = encryptedString(key, encodeURIComponent($("#exCvn2").val()));
        formData.exCvn2 = result;
    }
    if($('#exValidDate').length && $('#exValidDate').length>0) {
        result = encryptedString(key, encodeURIComponent($("#exValidDate").val()));
        formData.exValidDate = result;
    }

    if($('#exHolder').length && $('#exHolder').length>0) {
        if (exHolder && exHolder != '') {
            result = encryptedString(key, encodeURIComponent(exHolder));
        } else {
            result = encryptedString(key, encodeURIComponent($("#exHolder").val()));
        }
        formData.exHolder = result;
    }
    if($('#exIdCardNo').length && $('#exIdCardNo').length>0) {
        if (exIdCardNo && exIdCardNo != '') {
            result = encryptedString(key, encodeURIComponent(exIdCardNo));
        } else {
            result = encryptedString(key, encodeURIComponent($("#exIdCardNo").val()));
        }
        formData.exIdCardNo = result;
    }

    if($('#exMobileNo').length && $('#exMobileNo').length>0) {
        result = encryptedString(key, encodeURIComponent($("#exMobileNo").val()));
        formData.exMobileNo = result;
    }
    if($('#bankCardNo').length && $('#bankCardNo').length>0) {
        result = encryptedString(key, encodeURIComponent($("#bankCardNo").val()));
        formData.bankCardNo = result;
    }

    jQuery.ajax({
    	type: 'POST', 
        url :ctx+"/00100/express/checkSms.shtml?t="+(new Date()).getTime(),
        async:false,
        dataType:"json",
        data:formData,
        success: function (data) {
            if(data.result && data.result == "1"){
                res = true;
            }else{
            	$("#exSmsCodeTip").html(data.retMsg).show();
            }
        }
    });
    if(!res)
        $("#exSmsCodeTip").show();
    return res;
}


function sendSms(sendSmsBtn,formId){
    $("#exSmsCodeTip").hide();
    var obj = $("#"+sendSmsBtn+"");
    obj.attr('disabled','disabled');
    
    var formData = $("#"+formId+"").getFormData();
    
    //处理隐藏字段
  //处理星号的字段 exholder  和    idcard
    var exHolder = $.trim($('#exHolder').attr('initData'));
    if (exHolder != '') {
    	formData.exHolder = exHolder;
    }
    
    var exIdCardNo = $.trim($('#exIdCardNo').attr('initData'));
    if (exIdCardNo != '') {
    	formData.exIdCardNo = exIdCardNo;
    }

    bodyRSA();
    var result;
    if($('#exCvn2').length && $('#exCvn2').length>0){
        result = encryptedString(key, encodeURIComponent($("#exCvn2").val()));
        formData.exCvn2 = result;
    }
    if($('#exValidDate').length && $('#exValidDate').length>0) {
        result = encryptedString(key, encodeURIComponent($("#exValidDate").val()));
        formData.exValidDate = result;
    }

    if($('#exHolder').length && $('#exHolder').length>0) {
        if (exHolder && exHolder != '') {
            result = encryptedString(key, encodeURIComponent(exHolder));
        } else {
            result = encryptedString(key, encodeURIComponent($("#exHolder").val()));
        }
        formData.exHolder = result;
    }
    if($('#exIdCardNo').length && $('#exIdCardNo').length>0) {
        if (exIdCardNo && exIdCardNo != '') {
            result = encryptedString(key, encodeURIComponent(exIdCardNo));
        } else {
            result = encryptedString(key, encodeURIComponent($("#exIdCardNo").val()));
        }
        formData.exIdCardNo = result;
    }
    if($('#exMobileNo').length && $('#exMobileNo').length>0) {
        result = encryptedString(key, encodeURIComponent($("#exMobileNo").val()));
        formData.exMobileNo = result;
    }
    if($('#bankCardNo').length && $('#bankCardNo').length>0) {
        result = encryptedString(key, encodeURIComponent($("#bankCardNo").val()));
        formData.bankCardNo = result;
    }
    
    $.post(ctx+"/00100/express/getSms.shtml", formData, function(data) {
        if(data.result && data.result == "1"){
            var max = 60;
            $(obj).attr('disabled','disabled');
            $(obj).attr('count',max);
            $(obj).prop("class","tips-6 zoom verticalTop").text('重新获取('+max+'s)');
            setTimeout('checkTimeRemain("'+sendSmsBtn+'")',1000);
        }else{
            $(obj).removeAttr('disabled');
            var msg = "获取短信验证码失败";
            if(data.retMsg!=null){
                msg = data.retMsg;
            }else if(data.errMsg =="2"){
                msg = "银行卡信息有误，验证码发送失败";
            }
            $("#exSmsCodeTip").html("").html(msg).show();
        }
    },'json');
}
function checkTimeRemain(objId){
    var obj = document.getElementById(objId);
    var c = new Number($(obj).attr('count'));
    var next = c-1;
    if(c <= 0){
        $(obj).removeAttr('disabled');
        $(obj).text($(obj).attr('title'));
        return;
    }else{
        $(obj).attr('count',next);
        if(next!=0){
            $(obj).prop("class","tips-6 zoom verticalTop").text('重新获取('+next+'s)');
        }else{
            $(obj).prop("class","tips-5 zoom verticalTop").text('免费获取');
        }
        setTimeout('checkTimeRemain("'+objId+'")',1000);
    }
}


function checkCardNo(ccardNo){
    $("#freezeTip").hide();
    ccardNo = ccardNo.replace(/\s+/g, "");
    if(!(/^\d{10,25}/.test(ccardNo))){
        return;
    }
    $.get(ctx+"/00100/express/cbin.shtml", {ccardNo: ccardNo}, function(data) {
    	
    	//add by dongdh 20160330
    	$("#expressPayBankImg").attr("src", '');
        $("#expressCardTypeTip").removeClass("zoom").removeClass("btn-1").html("");
    	
        if(data.cbin == "0"){//未知
            $("#payBankTr").show();
            $("#binTd").hide();
            $("#noBinTd").show();
            $("#exPayChannel").val("");
            $("#exPayBankCode").val("");
        }else{
            var bankCode = data.bankCode;
            var sysSupport = false;

            $("#binTd").show();
            $("#noBinTd").hide();

            //识别出但系统不支持
            if(data.cbin == "1" && hasContains(debitCardArray,bankCode)){
                $("#expressPayBankImg").attr("src",ctx+"/BankIcon/"+bankCode+".jpeg");
                $("#expressCardTypeTip").prop("class","btn-3 zoom").html("储蓄卡");
                $("#exPayChannel").val("03");
                $("#exPayBankCode").val(bankCode);
                sysSupport = true;
            }else if(data.cbin == "2" && hasContains(creditCardArray,bankCode)){
                $("#expressPayBankImg").attr("src",ctx+"/BankIcon/"+bankCode+".jpeg");
                $("#expressCardTypeTip").prop("class","btn-1 zoom").html("信用卡");
                $("#exPayChannel").val("06");
                $("#exPayBankCode").val(bankCode);
                sysSupport = true;
            }
            if(!sysSupport){
                $("#notSupportTip").show();
                
                //add by dondh 20160330
                var src = $('#binTd #expressPayBankImg').attr('src');
        		if (!src || src == '') {
        			$("#payBankTr").hide();
        		}

            }else{
                $("#payBankTr").show();
            }
        }
    },"json");
}

//下一步
var expressLocationSubmit = function(loginFlag,btnId){
    if($("#notSupportTip").css("display") != "none"){
        return false;
    }
    if(((changExCardFlag && loginFlag =="1" || (loginFlag =="0")) && $("#exPayWay").val()!="A" && $("#bankCardNo").val()=="")){
        $("#freezeTip").html("银行卡号输入错误").show();
        return false;
    }
    if(($("#exPayChannel").val()=="" || $("#exPayBankCode").val()=="") && $("#exPayWay").val()!="A"){
        if(loginFlag == "1") {
        	
        	//add by dongdh  20160330
    		var src = $('#binTd #expressPayBankImg').attr('src');
    		if (!src || src == '') {
    			$('#binTd').hide();
    			$('#noBinTd').show();
    		}
        	
            $("#payBankTr").show();
        }
        if($("#exCardId").length == 0 && $("#payBankTr").css("display") == "none"){
            $("#freezeTip").html("银行卡号输入错误").show();
            return false;
        }
        $("#selectPayBankTip1").show();
        $("#selectPayBankTip2").show();
        return false;
    }

    if(loginFlag == "1" && $("#exPayWay").val() == "A"){//余额支付
        $("#"+btnId).attr("disabled","disabled");
        $("#locationBankFrom").submit();
        $("#"+btnId).removeAttr("disabled");
    }else{
        var ccardNo = $("#bankCardNo").val().replace(/\s+/g, "");
        if($.trim(ccardNo).length < 15){
            $("#freezeTip").html("银行卡号不能小于15位。").show();
            return false;
        }
        if($.trim(ccardNo).length > 25){
            $("#freezeTip").html("银行卡号不能大于25位。").show();
            return false;
        }
        if(changExCardFlag) {
            $("#changExCardFlag").val("0");
        }else{
            $("#changExCardFlag").val("1");
        }

        bodyRSA();
        ccardNo = encryptedString(key, encodeURIComponent(ccardNo));
        $("#bankCardNo_rsa").val(ccardNo);

        $("#"+btnId).attr("disabled","disabled");
        $.get(ctx+"/00100/express/status.shtml", {ccardNo: ccardNo}, function(data) {
            $("#"+btnId).removeAttr("disabled");
            if(data.sta == "1"){
                $("#freezeTip").html("该卡号已被锁定，请更换其他银行卡。").show();
                $("#selectPayBankTip1").show();
                $("#selectPayBankTip11").html("该卡号已被锁定，请更换其他银行卡。");
                return false;
            }if(data.sta == "2"){
                $("#freezeTip").html("卡号不能为空。").show();
                $("#selectPayBankTip1").show();
                $("#selectPayBankTip11").html("卡号不能为空。");
                return false;
            }else{
                $("#locationBankFrom").submit();
            }
        },"json");
    }
};

var expressSwitchGopay = function(){
    if($("#gopayChannelSel").length>0){
        $(".tabsUl .tabsLabel").removeClass("cur");
        $("#gopayChannelSel").addClass("cur");
        showChannel("06");
    }
}

var hasContains = function(array,value){
    return jQuery.inArray(value,array) != -1;
}

var changeExCard = function () {
    $("#exCardListDiv").hide();
    $("#firstExpressView").show();
    $("#payBankTr").hide();
    changExCardFlag = true;
    //删除已选中
    $('input[name="exCardId"]:checked').removeAttr("checked");
    $("#exPayChannel").val("F");
    
    if (bankAcctNum != '') {
    	 $('#bankCardNo').val(bankAcctNum)
    	$('#bankCardNo').trigger('input');
    	$('#bankCardNo').trigger('propertychange');
    } else {
    	$("#exPayBankCode").val("");
    	$("#bankCardNo").val("");
    }
    avalPayCheckInit();
}

var exBankSel = function (index,exPayChannel,exPayBankCode,bankCardNo) {
    $("#selectPayBankTip1").hide();
    $("#selectPayBankTip2").hide();
    $("#exCardId_"+index+"").prop("checked", true);
    $("#exPayChannel").val(exPayChannel);
    $("#exPayBankCode").val(exPayBankCode);
    $("#bankCardNo").val(bankCardNo);
    avalPayCheckInit();
}

function avalPayCheckInit(){
	var yesAvalPay = document.getElementById('yesAvalPay');
    if(yesAvalPay) {
    	yesAvalPay.checked = false;
    }
    $("#payWay").val("F");
    $("#exPayWay").val("F");
}

//登录支付确认---------------------
function exPayConfirmSendSms(sendSmsTip,reSendSmsTip,form){
    if(commonPayCheckNoSms()){
        sendSms(sendSmsTip,reSendSmsTip,form);
    }
}

function exGopayConfirmSubmit(formId){
    $('#snd').val(GopayDtCert.sign());

    $("#confirmSubmit").attr("disabled","disabled");
    if ($("#exCardId").val()!='' ||  $("#exPayWay").val()=='A'){
        if(!microdoneTip("expressGopayPwdCtrlTip")){
            $("#confirmSubmit").removeAttr("disabled");
            return false;
        }
    }

    bodyRSA();
    var result;
    if($('#exCvn2').length && $('#exCvn2').length>0){
        result = encryptedString(key, encodeURIComponent($("#exCvn2").val()));
        $("#exCvn2_rsa").val(result);
    }
    if($('#exValidDate').length && $('#exValidDate').length>0) {
        result = encryptedString(key, encodeURIComponent($("#exValidDate").val()));
        $("#exValidDate_rsa").val(result);
    }

    if($('#exHolder').length && $('#exHolder').length>0) {
        result = encryptedString(key, encodeURIComponent($("#exHolder").val()));
        $("#exHolder_rsa").val(result);
    }
    if($('#exIdCardNo').length && $('#exIdCardNo').length>0) {
        result = encryptedString(key, encodeURIComponent($("#exIdCardNo").val()));
        $("#exIdCardNo_rsa").val(result);
    }
    if($('#exMobileNo').length && $('#exMobileNo').length>0) {
        result = encryptedString(key, encodeURIComponent($("#exMobileNo").val()));
        $("#exMobileNo").val(result);
    }
    if($('#bankCardNo').length && $('#bankCardNo').length>0) {
        result = encryptedString(key, encodeURIComponent($("#bankCardNo").val()));
        $("#bankCardNo").val(result);
    }

    $("#"+formId+"").submit();
}

var key;
function bodyRSA() {
    setMaxDigits(130);
    key = new RSAKeyPair($("#empoent").val(),"",$("#module").val());
}

function exConfirmSubmit(formId,exPayChannel){
	$("#expressGopayPwdCtrlTip").hide();

    var check = false;
    if($("#loginExFirstPay").val() == "01"){
        if($("#checkAgreeOpen").length>0 && $("#checkAgreeOpen").val() == "1"){
            check = commonPayCheck() && checkAgreeOpen()&&checkSms(formId) ;  //登陆首次快捷校验短信的逻辑放在前段校验
        }else{
            check = commonPayCheck()&& checkSms(formId) ;
        }
    }else{
        if(exPayChannel == "06"){
            if(!(check = checkExCvn2())){
            	return;
            }
        }
        
        // 快捷支付改造修改  add by dhj at 20161201 start
        check = checkExSmsCode();
        // 快捷支付改造修改  add by dhj at 20161201 end
    }
    if(check){
        exGopayConfirmSubmit(formId);
    }
}
//---------------

//-------------首次支付--------------
function firstPayCheck(btnId,formId){
    if(commonPayCheck() && checkAgreeOpen() && checkSms(formId)){
        $("#"+btnId).attr("disabled","disabled");
        
        //处理星号的字段 exholder  和    idcard
        var exHolder = $.trim($('#exHolder').attr('initData'));
        var exIdCardNo = $.trim($('#exIdCardNo').attr('initData'));
        /*if (exHolder != '') {
        	$('#exHolder').attr('name', 'exHolder_temp');
        	$("#"+formId+"").append("<input type='hidden'  name='exHolder' value= '" + exHolder + "'>");
        }

        if (exIdCardNo != '') {
        	$('#exIdCardNo').attr('name', 'exIdCardNo_temp');
        	$("#"+formId+"").append("<input type='hidden' name='exIdCardNo' value= '" + exIdCardNo + "'>");
        }*/
        //alert($("#"+formId+"").html());

        bodyRSA();
        var result;
        if($('#exCvn2').length && $('#exCvn2').length>0){
            result = encryptedString(key, encodeURIComponent($("#exCvn2").val()));
            $("#exCvn2_rsa").val(result);
        }
        if($('#exValidDate').length && $('#exValidDate').length>0) {
            result = encryptedString(key, encodeURIComponent($("#exValidDate").val()));
            $("#exValidDate_rsa").val(result);
        }

        /*if($('#exHolder').length && $('#exHolder').length>0) {
            result = encryptedString(key, encodeURIComponent($("#exHolder").val()));
            $("#exHolder_rsa").val(result);
        }
        if($('#exIdCardNo').length && $('#exIdCardNo').length>0) {
            result = encryptedString(key, encodeURIComponent($("#exIdCardNo").val()));
            $("#exIdCardNo_rsa").val(result);
        }*/


        if($('#exHolder').length && $('#exHolder').length>0) {
            if (exHolder && exHolder != '') {
                result = encryptedString(key, encodeURIComponent(exHolder));
            } else {
                result = encryptedString(key, encodeURIComponent($("#exHolder").val()));
            }
            $("#exHolder_rsa").val(result);
        }
        if($('#exIdCardNo').length && $('#exIdCardNo').length>0) {
            if (exIdCardNo && exIdCardNo != '') {
                result = encryptedString(key, encodeURIComponent(exIdCardNo));
            } else {
                result = encryptedString(key, encodeURIComponent($("#exIdCardNo").val()));
            }
            $("#exIdCardNo_rsa").val(result);
        }



        if($('#exMobileNo').length && $('#exMobileNo').length>0) {
            result = encryptedString(key, encodeURIComponent($("#exMobileNo").val()));
            $("#exMobileNo_rsa").val(result);
        }
        if($('#bankCardNo').length && $('#bankCardNo').length>0) {
            result = encryptedString(key, encodeURIComponent($("#bankCardNo").val()));
            $("#bankCardNo").val(result);
        }

        $("#"+formId+"").submit();
    }
}

function firstSendSms(sendSmsTip,reSendSmsTip,form){
    if(commonPayCheckNoSms()){
        sendSms(sendSmsTip,reSendSmsTip,form);
    }
}
//---------------

function reUseBindCard(){
    changExCardFlag = false;
    $("#exCardListDiv").show();
    $("#firstExpressView").hide();
}